package com.usian.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.ai.common.Result;
import com.usian.ai.common.enums.ResultCodeEnum;
import com.usian.ai.common.exception.exception.PasswordErrorException;
import com.usian.ai.common.exception.exception.UsernameNotExistException;
import com.usian.ai.common.vo.LoginReqVo;
import com.usian.ai.mapper.AdminMapper;
import com.usian.ai.pojo.Admin;
import com.usian.ai.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 12 03 09 59
 **/

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    @Autowired
    AdminMapper adminMapper;

    /**
     *{
     *     "code":301,
     *     "msg":"登录成功"
     *}
     * {
     *     "code":501,
     *     "msg":"用户名不存在"
     * }
     * @param loginReqVo
     * @return
     */
    @Override
    public Result login(LoginReqVo loginReqVo) {

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>();
        queryWrapper.lambda().eq(Admin::getUsername,loginReqVo.getUsername());
        //queryWrapper.eq("username",loginReqVo.getUsername());
        Admin admin = adminMapper.selectOne(queryWrapper);
        //用户名不存在
        if(null==admin){
            log.info("用户名不存在");

            throw new UsernameNotExistException(ResultCodeEnum.USERNAME_NOT_EXIST);
        }
        String password = admin.getPassword();
        //密码不正确
        if(!password.equals(loginReqVo.getPassword())){
            log.info("密码不正确");
            throw new PasswordErrorException(ResultCodeEnum.PASSWORD_ERROR);

        }
        log.info("成功--------");


        return Result.ok(admin);
    }
}
